#####################################################################
# $Id: Epiphyte.pm,v 1.1.1.1 2006/03/20 04:08:45 jfb Exp $
#
# Epiphyte.pm - base module for the Epiphyte engine
#
#
#####################################################################

package Epiphyte;

use strict;
use vars qw($VERSION @ISA $CONFIG);

use HTTP::Status;
use HTTP::Request;
use HTTP::Response;

use Epiphyte::DB;
use Epiphyte::Log;
use Epiphyte::File;
use Epiphyte::Config;

# Current Epiphyte engine version
$VERSION = '0.01';


@ISA = qw(Exporter);


my @weekdays = qw(Sun Mon Tue Wed Thu Fri Sat);
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

my $canopy_log;
my $canopy_conf;


#####################################################################   
# init - class method
#
# Initialize the Epiphyte engine.
#####################################################################

sub init
{
	my ($class, $config) = @_;

	$canopy_log = new Epiphyte::Log('Epiphyte');

	Epiphyte->log(LOG_INFO, "initializing Epiphyte engine");

	$canopy_conf = Epiphyte::Config->load($config);
	if (!$canopy_conf) {
		Epiphyte->log(LOG_INFO, "failed to load configuration");
		return;
	}

	if (!Epiphyte::DB->open()) {
		return;
	}

	return 1;
}

#####################################################################   
# log - class method
#
#####################################################################   

sub log
{
	my ($class, $level, $msg, @args) = @_;

	$canopy_log->log($level, $msg, @args);
}

#####################################################################   
# getvar - class method
#
#####################################################################   

sub getvar
{
	my ($class, $var) = @_;

	return $canopy_conf->getvar($var);
}

#####################################################################   
# setvar - class method
#
#####################################################################   

sub setvar
{
	my ($class, $var, $val) = @_;

	return $canopy_conf->setvar($var, $val);
}

#####################################################################   
# req_handler - class method
#
# Entry point for requests from the Web server.  The handler
# expects an HTTP::Request object and generates and HTTP::Response
# object for the request.
#####################################################################

sub req_handler
{
	my ($class, $req) = @_;
	my $resp;

	if (ref($req) ne 'HTTP::Request') {
		Epiphyte->log(LOG_ERR, "request not an HTTP::Request object");

		$resp = new HTTP::Response(RC_INTERNAL_SERVER_ERROR);
		return ($resp);
	}

	my $uri = $req->uri();

	Epiphyte->log(LOG_INFO, "request for ", $req->uri());

	my $file = Epiphyte::File->load($uri->path(), 0);
	my $content = $file->parse($req);

	$resp = new HTTP::Response(RC_OK, 'OK');

	$resp->header('Content-Type' => 'text/html');
	$resp->header('Content-Length' => length($content));
	$resp->content($content);

	# Set the Date header
	my @fld = gmtime(time());
	my $date = sprintf("%s, %d %s %d %02d:%02d:%02d GMT",
	    $weekdays[$fld[6]], $fld[3], $months[$fld[4]], $fld[5] + 1900,
	    $fld[2], $fld[1], $fld[0]);
	$resp->header('Date' => $date);
 
	return ($resp);
}

1;
